package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.JFGLBean;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;

/**
 * Created by D N on 2017/10/17.
 */

public class JFGLActivity extends BaseActivity {
    @Bind(R.id.textView77)
    TextView textView77;
    @Bind(R.id.my_jfgl_bsfh)
    ImageView myJfglBsfh;
    @Bind(R.id.my_jfgl_jfsm)
    TextView myJfglJfsm;
    @Bind(R.id.jfgl_recyclerview)
    RecyclerView jfglRecyclerview;
    @Bind(R.id.jfgl_fs_text)
    TextView jfglFsText;
    @Bind(R.id.jfgl_smart)
    SmartRefreshLayout jfglSmart;


    private int current_page = 1;//列表当前页
    private int per_page = 10;//默认展示条数

    JFGLBean mYcollectionbean;
    ArrayList<JFGLBean.DataBean.ListBean> mdata = new ArrayList<>();
    BaseAdapter<JFGLBean.DataBean.ListBean> adapter;
    ArrayList<String> refer_id = new ArrayList<String>() {
        @Override
        public String toString() {
            String s = "";
            for (String info : refer_id) {
                s = s + info + ",";
            }
            return s;
        }
    };
    private View footerview;
    private boolean isFirstEnter = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jfgl_activity);
        ButterKnife.bind(this);
        footerview = LayoutInflater.from(this).inflate(R.layout.view_wd_footer, null);
        footerview.setVisibility(View.INVISIBLE);
        SampleApplicationLike.getActivitiesInstance().add(this);


        initRefresh();


    }

    private void initRefresh() {
        if (isFirstEnter) {
            jfglSmart.autoRefresh();
        }
        jfglSmart.setEnableLoadMore(true);//是否启用上拉加载功能
        jfglSmart.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        jfglSmart.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //refreshlayout.setLoadmoreFinished(false);//恢复上拉状态
                // Toasts.show(getActivity(), "加载更多啊", Toast.LENGTH_SHORT);
                requestData(1);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current_page = 1;
                requestData(0);
                //refreshlayout.finishRefresh();
            }
        });
    }

    private void requestData(final int isRefreshOrMore) {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("current_page", current_page + "");
        map.put("per_page", per_page + "");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, JFGLBean.class, URLAddress.URLBASE + URLAddress.URL_JFLB, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                mYcollectionbean = (JFGLBean) response;
                if (mYcollectionbean.getResult() == 1 && current_page == 1 && isRefreshOrMore == 0) {
                    mdata.clear();
                    mdata.addAll(mYcollectionbean.getData().getList());
                    current_page = current_page + 1;
                    initView();
                    jfglSmart.finishRefresh();
                } else if (mYcollectionbean.getResult() == 1&& isRefreshOrMore == 1) {
                    current_page = current_page + 1;
                    mdata.addAll(mYcollectionbean.getData().getList());
                    adapter.notifyDataSetChanged();
                    jfglSmart.finishLoadmore();
                    //footerview.setVisibility(View.INVISIBLE);
                }
                if (mYcollectionbean.getData().getList().size() == 0) {
                    jfglSmart.finishLoadmore();
                   // Toasts.show(JFGLActivity.this, "没有数据了", Toast.LENGTH_SHORT);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //footerview.setVisibility(View.INVISIBLE);
                //Toasts.show(JFGLActivity.this, "没有数据了", Toast.LENGTH_SHORT);
                jfglSmart.finishLoadmore();
                jfglSmart.finishRefresh();
            }
        });
    }


    private void initView() {
        jfglFsText.setText("" + mYcollectionbean.getData().getIntegral());
        adapter = new BaseAdapter<JFGLBean.DataBean.ListBean>(this, mdata) {
            @Override
            public void convert(int position, BaseViewHolder holder, JFGLBean.DataBean.ListBean referBean) {
                if (position % 2 == 0) {
                    holder.getView(R.id.jfgl_ll).setBackgroundColor(getResources().getColor(R.color.white));
                } else {
                    holder.getView(R.id.jfgl_ll).setBackgroundColor(getResources().getColor(R.color.hui_jfgl));
                }

                ((TextView) holder.getView(R.id.jfgl_text_time)).setText(referBean.getCreate_time());
                ((TextView) holder.getView(R.id.jfgl_text_fs_item)).setText(referBean.getNumber());
                ((TextView) holder.getView(R.id.jfgl_text_yy)).setText(referBean.getContent());
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.view_jfgl_item;
            }
        };

        //com.example.administrator.szb.adapter.addFootView(footerview);
        jfglRecyclerview.setLayoutManager(new LinearLayoutManager(JFGLActivity.this));
        jfglRecyclerview.setAdapter(adapter);
        //loadMore();
    }




    @OnClick({R.id.my_jfgl_bsfh, R.id.my_jfgl_jfsm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_jfgl_bsfh:
                finish();
                break;
            case R.id.my_jfgl_jfsm:
                startActivity(new Intent(JFGLActivity.this, JFSMActivity.class));
                break;
        }
    }
}
